﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Components;
using Components.Enum;
using Model;
using ServiceHelper.Util;

namespace ServiceHelper.Doc
{
    public class AcceptManager : DocumentManager
    {
        public override Sto8a00 CreateDefaultModel()
        {
            var model = new Sto8a00();
            model.Doctype = DocumentType.PurchaseAccept;
            return model;
        }

        public override string Save()
        {
            if (0 == Doc.Rows.Count)
                return "本次收货量为0，无需保存";

            return base.Save();
        }

        public Sto8a01 ParseRow(DataRow row)
        {
            var model = new Sto8a01();

            model.Merchid = Validation.ToString(row["pdlistid"]);
            model.Dtlrefno1 = Validation.ToString(row["listid"]);
            model.Startbuyqty = Validation.ToDouble(row["bordqty"]);
            model.Dtlqty = Validation.ToDouble(row["dtlqty"]); //本次发货量
            model.Actqty2 = Validation.ToDouble(row["actqty2"]); //发货赠品数
            model.Dtlprice = Validation.ToDouble(row["price"]); //订购价
            model.Cess = Validation.ToDouble(row["cess"]);
            model.Dtlremark = Validation.ToString(row["dtlremark"]);
            model.Actamt3 = Validation.ToDouble(row["dtltotalamt"]); //含税总额

            model.Dtlstockqty = model.Dtlqty + model.Actqty2; //用于记账的库存

            if (Validation.ToFloat(model.Dtlstockqty) == 0 && Validation.ToFloat(model.Dtlqty) == 0) //没有输入数量的行跳过
            {
                return null;
            }

            Doc.Rows.Add(model);

            return model;
        }

        /// <summary>
        /// 将概要信息转换成Sto8a00
        /// </summary>
        /// <param name="tb"></param>
        public Sto8a00 ParseDoc(Hashtable tb)
        {
            var sto8A00 = this.CreateDefaultModel();
            sto8A00.Warehouseid = Validation.ToString(tb["slistid"]);

            if (string.IsNullOrEmpty(sto8A00.Warehouseid))
                throw new Exception("未设置收货仓库，不能做收货");

            sto8A00.Docno = Validation.ToString(tb["arrno"]);

            if (!string.IsNullOrEmpty(sto8A00.Docno))
            {
                var qb = QB.Entity.AddParam("docno", sto8A00.Docno)
                                  .AddParam("doctype", DocumentType.PurchaseAccept)
                                  .AddParam("%usercode", string.Format(" like '{0}.%' ", ServerContext.ComCode));

                //验证发货单重复性
                if (CommonManager.Entity.HasData("Sto8a00", qb))
                {
                    var hsWhere = QB.Entity.AddParam("muser", ServerContext.ComCode + ".");

                    var lastno = CommonManager.Entity.ExecDbFunScalar("getlastacceptno", hsWhere);

                    var o = new { status = false, msg = string.Format("收货单号{0}重复，请重新输入", sto8A00.Docno), last = lastno };
                    throw new Exception(Json.ToString(o));
                }
            }
            else
            {
                CreateDocno(sto8A00);
            }

            sto8A00.Refid = Validation.ToString(tb["listid"]);
            sto8A00.Refdocno = Validation.ToString(tb["docno"]);
            sto8A00.Docstatus = DocumentStatus.Normal;
            //sto8A00.Refdocno2 = Validation.ToString(tb["arrno"]); //收货号
            sto8A00.Docdate = Convert.ToDateTime(tb["arrdate"]);  //收货时间

            return sto8A00;
        }

        protected override void AfterSubmit()
        {
            var exec = Exec.Create();

            int count = 0;
            Buy3a01 dtlModel;
            foreach (var dr in Doc.Rows)
            {
                count++;
                dtlModel = new Buy3a01();
                dtlModel.Listid = Convert.ToInt32(dr.Dtlrefno1);
                dtlModel.Accqty = dr.Dtlqty;
                dtlModel.Accpreqty = dr.Actqty2;
                dtlModel.Accamt = dr.Actamt3;

                //调用存储过程进行保存
                exec.AddSql(Buy3a00Manager.Entity.Arrival(count == Doc.Rows.Count ? "Y" : "N", dtlModel, Doc.Docno, Doc.Docdate.Value));
            }

            exec.Do();
        }
    }
}
